package com.ruoyi.constituent.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.constituent.domain.ConstituentData;
import com.ruoyi.constituent.service.IConstituentDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成分Controller
 * 
 * @author weihanqiang
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/constituent/data")
public class ConstituentDataController extends BaseController
{
    @Autowired
    private IConstituentDataService constituentDataService;

    /**
     * 查询成分列表
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstituentData constituentData)
    {
        startPage();
        List<ConstituentData> list = constituentDataService.selectConstituentDataList(constituentData);
        return getDataTable(list);
    }

    /**
     * 导出成分列表
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:export')")
    @Log(title = "成分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConstituentData constituentData)
    {
        List<ConstituentData> list = constituentDataService.selectConstituentDataList(constituentData);
        ExcelUtil<ConstituentData> util = new ExcelUtil<ConstituentData>(ConstituentData.class);
        util.exportExcel(response, list, "成分数据");
    }

    /**
     * 获取成分详细信息
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:query')")
    @GetMapping(value = "/{constituentId}")
    public AjaxResult getInfo(@PathVariable("constituentId") Long constituentId)
    {
        return success(constituentDataService.selectConstituentDataByConstituentId(constituentId));
    }

    /**
     * 新增成分
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:add')")
    @Log(title = "成分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstituentData constituentData)
    {
        constituentData.setCreatorId(getUserId());
        constituentData.setCreator(getUsername());
        return toAjax(constituentDataService.insertConstituentData(constituentData));
    }

    /**
     * 修改成分
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:edit')")
    @Log(title = "成分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstituentData constituentData)
    {
        constituentData.setModifierId(getUserId());
        constituentData.setModifier(getUsername());
        return toAjax(constituentDataService.updateConstituentData(constituentData));
    }

    /**
     * 删除成分
     */
    @PreAuthorize("@ss.hasPermi('constituent:data:remove')")
    @Log(title = "成分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{constituentIds}")
    public AjaxResult remove(@PathVariable Long[] constituentIds)
    {
        return toAjax(constituentDataService.deleteConstituentDataByConstituentIds(constituentIds));
    }
}
